package com.ldatum.crm.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ldatum.crm.dao.TDepartmentDao;
import com.ldatum.crm.domain.TDepartment;
import com.ldatum.crm.util.HibernateUtil;

public class TDepartmentDaoImpl implements TDepartmentDao {

	@Override
	public List<TDepartment> list() {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TDepartment as department";
			Query q = s.createQuery(hql);
			@SuppressWarnings("unchecked")
			List<TDepartment> list = (List<TDepartment>) q.list();
			return list;
		}finally{
			if(s!=null){
				s.close();
			}

		}
	}

	@Override
	public void save(TDepartment department) {
		HibernateUtil.add(department);
		
	}

	@Override
	public int deletes(String ids) {
		Session s = null;
		Transaction tx = null;
		ids = ids.substring(0,ids.length()-1);
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			String hql = "delete from TDepartment as department where id in ("+ids+")";
			Query q = s.createQuery(hql);
			int b = q.executeUpdate();
			tx.commit();
			return b;
		}finally{
			if(s!=null)
			s.close();
		}
	}

}
